@@ -14,7 +14,7 @@ class Event < ActiveRecord::Base |
||
| 14 | 14 |
json_serialize :payload |
| 15 | 15 |
|
| 16 | 16 |
belongs_to :user |
| 17 |
- belongs_to :agent, :counter_cache => true, :touch => :last_event_at |
|
| 17 |
+ belongs_to :agent, :counter_cache => true |
|
| 18 | 18 |
|
| 19 | 19 |
has_many :agent_logs_as_inbound_event, :class_name => "AgentLog", :foreign_key => :inbound_event_id, :dependent => :nullify |
| 20 | 20 |
has_many :agent_logs_as_outbound_event, :class_name => "AgentLog", :foreign_key => :outbound_event_id, :dependent => :nullify |
@@ -23,12 +23,13 @@ class Event < ActiveRecord::Base |
||
| 23 | 23 |
where("events.created_at > ?", timespan)
|
| 24 | 24 |
} |
| 25 | 25 |
|
| 26 |
+ after_create :update_agent_last_event_at |
|
| 27 |
+ after_create :possibly_propagate |
|
| 28 |
+ |
|
| 26 | 29 |
scope :expired, lambda {
|
| 27 | 30 |
where("expires_at IS NOT NULL AND expires_at < ?", Time.now)
|
| 28 | 31 |
} |
| 29 | 32 |
|
| 30 |
- after_create :possibly_propagate |
|
| 31 |
- |
|
| 32 | 33 |
# Emit this event again, as a new Event. |
| 33 | 34 |
def reemit! |
| 34 | 35 |
agent.create_event :payload => payload, :lat => lat, :lng => lng |
@@ -44,6 +45,10 @@ class Event < ActiveRecord::Base |
||
| 44 | 45 |
|
| 45 | 46 |
protected |
| 46 | 47 |
|
| 48 |
+ def update_agent_last_event_at |
|
| 49 |
+ agent.touch :last_event_at |
|
| 50 |
+ end |
|
| 51 |
+ |
|
| 47 | 52 |
def possibly_propagate |
| 48 | 53 |
#immediately schedule agents that want immediate updates |
| 49 | 54 |
propagate_ids = agent.receivers.where(:propagate_immediately => true).pluck(:id) |
@@ -0,0 +1,11 @@ |
||
| 1 |
+class SetEventsCountDefault < ActiveRecord::Migration |
|
| 2 |
+ def up |
|
| 3 |
+ change_column_default(:agents, :events_count, 0) |
|
| 4 |
+ change_column_null(:agents, :events_count, false, 0) |
|
| 5 |
+ end |
|
| 6 |
+ |
|
| 7 |
+ def down |
|
| 8 |
+ change_column_null(:agents, :events_count, true) |
|
| 9 |
+ change_column_default(:agents, :events_count, nil) |
|
| 10 |
+ end |
|
| 11 |
+end |
@@ -11,7 +11,7 @@ |
||
| 11 | 11 |
# |
| 12 | 12 |
# It's strongly recommended that you check this file into your version control system. |
| 13 | 13 |
|
| 14 |
-ActiveRecord::Schema.define(version: 20140820003139) do |
|
| 14 |
+ActiveRecord::Schema.define(version: 20140906030139) do |
|
| 15 | 15 |
|
| 16 | 16 |
# These are extensions that must be enabled in order to support this database |
| 17 | 17 |
enable_extension "plpgsql" |
@@ -32,7 +32,7 @@ ActiveRecord::Schema.define(version: 20140820003139) do |
||
| 32 | 32 |
t.string "type", collation: "utf8_bin" |
| 33 | 33 |
t.string "name", charset: "utf8mb4", collation: "utf8mb4_bin" |
| 34 | 34 |
t.string "schedule", collation: "utf8_bin" |
| 35 |
- t.integer "events_count" |
|
| 35 |
+ t.integer "events_count", default: 0, null: false |
|
| 36 | 36 |
t.datetime "last_check_at" |
| 37 | 37 |
t.datetime "last_receive_at" |
| 38 | 38 |
t.integer "last_checked_event_id" |
@@ -39,12 +39,7 @@ bob_weather_agent: |
||
| 39 | 39 |
name: "SF Weather" |
| 40 | 40 |
guid: <%= SecureRandom.hex %> |
| 41 | 41 |
keep_events_for: 45 |
| 42 |
- options: <%= {
|
|
| 43 |
- :location => 94102, |
|
| 44 |
- :lat => 37.779329, |
|
| 45 |
- :lng => -122.41915, |
|
| 46 |
- :api_key => 'test' |
|
| 47 |
- }.to_json.inspect %> |
|
| 42 |
+ options: <%= { :location => 94102, :lat => 37.779329, :lng => -122.41915, :api_key => 'test' }.to_json.inspect %>
|
|
| 48 | 43 |
|
| 49 | 44 |
jane_weather_agent: |
| 50 | 45 |
type: Agents::WeatherAgent |
@@ -53,12 +48,7 @@ jane_weather_agent: |
||
| 53 | 48 |
name: "SF Weather" |
| 54 | 49 |
guid: <%= SecureRandom.hex %> |
| 55 | 50 |
keep_events_for: 30 |
| 56 |
- options: <%= {
|
|
| 57 |
- :location => 94103, |
|
| 58 |
- :lat => 37.779329, |
|
| 59 |
- :lng => -122.41915, |
|
| 60 |
- :api_key => 'test' |
|
| 61 |
- }.to_json.inspect %> |
|
| 51 |
+ options: <%= { :location => 94103, :lat => 37.779329, :lng => -122.41915, :api_key => 'test' }.to_json.inspect %>
|
|
| 62 | 52 |
|
| 63 | 53 |
jane_rain_notifier_agent: |
| 64 | 54 |
type: Agents::TriggerAgent |
@@ -87,6 +87,34 @@ describe Event do |
||
| 87 | 87 |
agent_logs(:log_for_bob_website_agent).reload.outbound_event_id.should be_nil |
| 88 | 88 |
end |
| 89 | 89 |
end |
| 90 |
+ |
|
| 91 |
+ describe "caches" do |
|
| 92 |
+ describe "when an event is created" do |
|
| 93 |
+ it "updates a counter cache on agent" do |
|
| 94 |
+ lambda {
|
|
| 95 |
+ agents(:jane_weather_agent).events.create!(:user => users(:jane)) |
|
| 96 |
+ }.should change { agents(:jane_weather_agent).reload.events_count }.by(1)
|
|
| 97 |
+ end |
|
| 98 |
+ |
|
| 99 |
+ it "updates last_event_at on agent" do |
|
| 100 |
+ lambda {
|
|
| 101 |
+ agents(:jane_weather_agent).events.create!(:user => users(:jane)) |
|
| 102 |
+ }.should change { agents(:jane_weather_agent).reload.last_event_at }
|
|
| 103 |
+ end |
|
| 104 |
+ end |
|
| 105 |
+ |
|
| 106 |
+ describe "when an event is updated" do |
|
| 107 |
+ it "does not touch the last_event_at on the agent" do |
|
| 108 |
+ event = agents(:jane_weather_agent).events.create!(:user => users(:jane)) |
|
| 109 |
+ |
|
| 110 |
+ agents(:jane_weather_agent).update_attribute :last_event_at, 2.days.ago |
|
| 111 |
+ |
|
| 112 |
+ lambda {
|
|
| 113 |
+ event.update_attribute :payload, { 'hello' => 'world' }
|
|
| 114 |
+ }.should_not change { agents(:jane_weather_agent).reload.last_event_at }
|
|
| 115 |
+ end |
|
| 116 |
+ end |
|
| 117 |
+ end |
|
| 90 | 118 |
end |
| 91 | 119 |
|
| 92 | 120 |
describe EventDrop do |